package com.example.fuzhuang.services;

import com.alibaba.fastjson.JSONObject;
import com.example.fuzhuang.dto.*;
import com.example.fuzhuang.exception.BusinessException;
import com.example.fuzhuang.exception.BusinessExceptionCode;
import com.example.fuzhuang.mapper.UserMapper;
import com.example.fuzhuang.req.UserReq;
import com.example.fuzhuang.resp.*;
import com.example.fuzhuang.utils.CopyUtil;
import com.example.fuzhuang.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {
    private static final Logger LOG = LoggerFactory.getLogger(UserService.class);

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserMapper userMapper;

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private CaptchaService captchaService;

    public UserResp login(UserReq req){
        LOG.info(req.getUsername());
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(req.getUsername());
        criteria.andPasswordEqualTo(req.getPassword());

        List<User> userList = userMapper.selectByExample(userExample);

        if(!captchaService.validateCaptcha(req.getSessionId(),req.getCode())){
            throw new BusinessException(BusinessExceptionCode.CODE_ERROR);
        } else if(userList.isEmpty()){
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }else{
            User user = userList.get(0);
            UserResp loginUserResp = CopyUtil.copy(user, UserResp.class);

            Long id = snowFlake.nextId();
            redisTemplate.opsForValue().set(id,JSONObject.toJSONString(user),3600*24, TimeUnit.SECONDS);
            loginUserResp.setToken(id);

            return loginUserResp;
        }


    }

    public void logout(String token){
        redisTemplate.delete(token);
    }

    public PageResp<UserResp> list(UserReq req) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if(req.getName() != null) {
            criteria.andNameLike("%" + req.getName() + "%");
        }

        PageHelper.startPage(req.getPage(),req.getSize());
        List<User> userList = userMapper.selectByExample(userExample);

        PageInfo<User> pageInfo = new PageInfo<>(userList);
        PageResp<UserResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setPages(pageInfo.getPages());

        List<UserResp> tagRespList = CopyUtil.copyList(userList,UserResp.class);

        pageResp.setList(tagRespList);
        return pageResp;
    }

    public User save(UserReq req){

        User user = CopyUtil.copy(req, User.class);
        if(req.getId() == null){
            Long id = snowFlake.nextId();
            user.setId(id);
            userMapper.insert(user);
        }else {
            userMapper.updateByPrimaryKeySelective(user);
        }

        return user;

    }

    public void delete(UserReq req){
        userMapper.deleteByPrimaryKey(req.getId());
    }

}
